package curd

import (
	"github.com/gin-gonic/gin"
	"goskeleton/app/model/project"
	departmentCurd "goskeleton/app/service/auth_system_menu"
	customerCurd "goskeleton/app/service/customer/curd"
	employeeCurd "goskeleton/app/service/employee/curd"
	// organizationCurd "goskeleton/app/service/organization/curd"
)

func CreateProjectCurdFactory() *ProjectCurd {
	return &ProjectCurd{ProjectModel: project.CreateProjectFactory("")}
}

type ProjectCurd struct {
	ProjectModel *project.ProjectModel
}

func (e *ProjectCurd) InsertData(c *gin.Context) bool {
	return e.ProjectModel.InsertData(c)
}

func (e *ProjectCurd) UpdateData(c *gin.Context) bool {
	return e.ProjectModel.UpdateData(c)
}

func (e *ProjectCurd) ListData(
	limits,
	limitItems int,
	order string,
	projectName string,
	responsibleEmployeeId int,
	responsibleDepartmentId int,
	progress int,
	expectedStartDateStart string,
	expectedStartDateEnd string,
	expectedEndDateStart string,
	expectedEndDateEnd string,
) (totalCounts int64, tmp []*project.ProjectModel) {
	authService := &departmentCurd.AuthSystemMenuService{}
	responsibleDepartmentIds := authService.GetAllChildIdsById(responsibleDepartmentId, 1)

	// 将 []uint 类型的 responsibleDepartmentIds 转换为 []int 类型
	responsibleDepartmentIdsInt := make([]int, len(responsibleDepartmentIds))
	for i, v := range responsibleDepartmentIds {
		responsibleDepartmentIdsInt[i] = int(v)
	}
	totalCounts, tmp = e.ProjectModel.ListData(
		limits,
		limitItems,
		order,
		projectName,
		responsibleEmployeeId,
		responsibleDepartmentIdsInt,
		progress,
		expectedStartDateStart,
		expectedStartDateEnd,
		expectedEndDateStart,
		expectedEndDateEnd)
	if totalCounts > 0 {

		// var clientIds,employeeIds, organizationIds []int // 客户id，员工id，部门id
		var clientIds, employeeIds, departmentIds []int // 客户id，员工id，部门id
		for _, v := range tmp {                         // 遍历项目列表，获取客户id，员工id，部门id
			clientIds = append(clientIds, v.ClientId)
			employeeIds = append(employeeIds, v.ResponsibleEmployeeId)
			departmentIds = append(departmentIds, v.ResponsibleDepartmentId)
		}

		// 获取客户列表
		customerCurd := customerCurd.CreateCustomerCurdFactory()
		customers := customerCurd.GetCustomerByIds(clientIds)

		customerMap := make(map[int]string)

		for _, customer := range customers {
			customerMap[int(customer.Id)] = customer.Name
		}

		// 获取员工列表
		employeeCurd := employeeCurd.CreateEmployeeCurdFactory()
		employees := employeeCurd.GetEmployeeByIds(employeeIds)

		employeeMap := make(map[int]string)

		for _, employee := range employees {
			employeeMap[int(employee.Id)] = employee.Name
		}

		// 获取部门列表
		departmentCurd := &departmentCurd.AuthSystemMenuService{}
		departments := departmentCurd.GetAllPostsByIds(departmentIds)

		departmentMap := make(map[int]string)

		for _, department := range departments {
			departmentMap[int(department.Id)] = department.Title
		}

		for _, project := range tmp {
			if project.ClientId != 0 {
				if name, exists := customerMap[project.ClientId]; exists {
					// 由于 project.ProjectModel 类型没有 CustomerName 字段，这里注释掉设置操作
					project.CustomerName = name // 设置客户名称
				}

			}
			if project.ResponsibleEmployeeId != 0 {
				if name, exists := employeeMap[project.ResponsibleEmployeeId]; exists {
					project.EmployeeName = name // 设置员工名称
				}
			}
			if project.ResponsibleDepartmentId != 0 {
				if name, exists := departmentMap[project.ResponsibleDepartmentId]; exists {
					project.DepartmentName = name // 设置部门名称
				}
			}
		}

		return totalCounts, tmp
	} else {
		return 0, nil
	}
}

func (e *ProjectCurd) ShowOneItem(id int) (tmp *project.ProjectModel) {
	project := e.ProjectModel.ShowOneItem(id)
	if project == nil {
		return nil
	}
	// 客户名称
	customerCurd := customerCurd.CreateCustomerCurdFactory()
	customer := customerCurd.ShowOneItem(project.ClientId)
	if customer != nil {
		project.CustomerName = customer.Name
	}
	// 员工名称
	employeeCurd := employeeCurd.CreateEmployeeCurdFactory()
	employee := employeeCurd.ShowOneItem(project.ResponsibleEmployeeId)
	if employee != nil {
		project.EmployeeName = employee.Name
	}
	// 部门名称
	departmentCurd := &departmentCurd.AuthSystemMenuService{}
	department := departmentCurd.GetFindById(project.ResponsibleDepartmentId)
	if department != nil {
		project.DepartmentName = department.Title
	}
	return project
}

func (e *ProjectCurd) GetProjectByIds(ids []int) (tmp []*project.ProjectModel) {
	return e.ProjectModel.GetProjectByIds(ids)
}

func (e *ProjectCurd) GetTotalProgressStatistics() (totalStatusStatistics *project.TotalStatusStatistics) {
	return e.ProjectModel.GetTotalProgressStatistics()
}

func (e *ProjectCurd) GetOneDataById(id int) (tmp *project.ProjectModel) {
	return e.ProjectModel.ShowOneItem(id)
}

func (e *ProjectCurd) GetAllProjectName() []*project.SearchProject {
	return e.ProjectModel.GetAllProjectName()
}

func (e *ProjectCurd) DeleteData(id int) bool {
	return e.ProjectModel.DeleteData(id)
}